<?php
/**
 * 商家账户信息类
 *
 */
class User_Merchant_Account 
{
	private $_merchant_uid;
	
	private $_merchant_ratio;
	private $_merchant_contact;
	private $_merchant_money;

	protected static $_instance = array();

	public function __construct($merchant_uid)
	{
		$this->_merchant_uid  = (int) $merchant_uid;
		
		$this->_merchant_money   = new User_Merchant_Account_Money($this->_merchant_uid);
	}

	/**
	 *	返回事件处理器
	 *
	 *	@param mixed $event
	 *	@param object $handler
	 */
	public static function event_handler($event)
	{
		$class	= __CLASS__;
		return new $class;
	}

	/**
	 * 商家账户金额
	 * 
	 * @param int $merchant_uid
	 *
	 * @return array(
	 *				'annual_fee'		=> int //单位：分 年费 
	 *				'technical_service'	=> int //单位：分 技术服务费 目前不用
	 *				'margin'			=> int //单位：分 保证金
	 *				'stored_service'	=> int //单位：分 预缴服务费
	 *				);  
	 *
	 * 返回数据的key值说明看 Kohana::config('account.money_type');
	 * 
	 * @return array()  : 不存在这样的商家;	
	 */
	public static function account_money($merchant_uid)
	{
		$merchant_uid = (int) $merchant_uid;

		if($merchant_uid <=0)
			return array();

		return self::instance('money')->get_money($merchant_uid);
	}

	/**
	 * 商家的账户 
	 *
	 * @param array $merchant_uids
	 * @param String $type  不传参数， 默认返回商家所有信息；  
	 *               如果只需要某类信息 看 Kohana::config('account.info_type')
	 *
	 * @return array(
	 *				'shop_id' => array(
	 *									'annual_fee' => int //单位： 分 年费 
	 *									'margin'	 => int //单位： 分 保证金
	 *									'stored_service' => int //单位： 分  预缴服务费
	 *									...
	 *									),
	 *				...
	 *				);
	 * 返回数据的商户金额key值说明看 Kohana::config('account.money_type');
	 * @return array()  此类商家相关信息不存在
	 */
	public static function account_info_lists($merchant_uids, $type = NULL)
	{
		if(empty($merchant_uids))
			return array();

		for($i=0; $i< count($merchant_uids); $i++)
		{
			$merchant_uid = $merchant_uids[$i];
							  		
			$data[$merchant_uid] = self::account_money($merchant_uid);
			
		}
		return $data;
	}

	/**
	 * 获得账户某种类型钱的充值信息
	 *
	 * @param int $merchant_uid
	 * @param String $money_type
	 *
	 * @return array(
	 *			array(
	 *				'shop_id'
	 *				'merchant_uid'
	 *				'money'			=> int //单位：分
	 *				'type'			=> int //钱的类型   Kohana::config('account.money_type.***')
	 *				'start_time'	=> int 
	 *				'end_time'		=> int
	 *				'status'		=> int // 合同状态 Kohana::config('account.contract_status') 
	 *				'flag'			=> int // 付款状态 Kohana::config('account.pay_status')
	 *				'invoice_time'	=> int //发票日期
	 *				'receipt_time'  => int //收据日期
	 *				'create_time'	=> int
	 *				'update_time'	=> int
	 *			),
	 *			...
	 *			)
	 *
	 */
	public function account_money_info($merchant_uid, $money_type)
	{
		$rs = $this->_merchant_money->get_money_info($merchant_uid, $money_type);

		return $rs;
	}

	/**
	 * 获得某个合同的充值情况
	 *
	 * @param int $contract_id
	 * @return array(
	 *				'pay_status' => int //付款状态
	 *				'pay_status_name' => string //付款状态描述
	 *			)
	 *
	 */
	public function contract_pay_status($contract_id)
	{
		$contract_id = (int) $contract_id;

		if($contract_id <= 0)
			return array();

		$rs	= $this->_merchant_money->is_haved($contract_id);

		if($rs == 0){
			$data = array('pay_status' => 0, 'pay_status_name' => '未支付');
		}elseif($rs >= 3){
			$data = array('pay_status' => 1, 'pay_status_name' => '已付款');
		}else
			$data = array('pay_status' => 2, 'pay_status_name' => '部分付款');

		return $data;
	}

	/**
	 * 获得某个合同的保证金的充值情况
	 *
	 * @param int $contract_id
	 * @return boolean 
	 *
	 */
	public function contract_margin_status($contract_id)
	{
		$contract_id = (int) $contract_id;

		if($contract_id <= 0)
			return array();

		return 	$this->_merchant_money->is_haved_margin($contract_id);
	}


	/**
	 * 获得某一条的充值信息
	 *
	 * @param int $id
	 *
	 * @return array(
	 *				'shop_id'
	 *				'merchant_uid'
	 *				'money'			=> int //单位：元
	 *				'type'			=> int //钱的类型   Kohana::config('account.money_type.***')
	 *				'start_time'	=> date 
	 *				'end_time'		=> date
	 *				'status'		=> int // 合同状态 Kohana::config('account.contract_status') 
	 *				'flag'			=> int // 付款状态 Kohana::config('account.pay_status')
	 *				'invoice_time'	=> date //发票日期
	 *				'receipt_time'  => date //收据日期
	 *				'create_time'	=> date
	 *				'update_time'	=> date
	 *			)
	 *
	 */
	public function account_one_info($id)
	{
		if(intval($id)<=0)
			return array();

		$rs = $this->_merchant_money->get_info($id);
		$contract = new Bill_Contract_Info();
		$contract_info = $contract->get_contract_by_id($rs['contract_id']);	

		$rs['contract_str'] = '<a href="/bill/contract_detail/'.$contract_info['id'].'" target=_blank>'.$contract_info['number'].'</a>';
		$rs['start_time']	= date('Y-m-d', $rs['start_time']);
		$rs['end_time']		= date('Y-m-d', $rs['end_time']);
		$rs['create_time']	= date('Y-m-d', $rs['create_time']);
		$rs['update_time']	= date('Y-m-d', $rs['update_time']);
		$rs['invoice_time'] = empty($rs['invoice_time']) ? '' : date('Y-m-d', $rs['invoice_time']);
		$rs['receipt_time'] = date('Y-m-d', $rs['receipt_time']);
		$rs['money']		= (int) $rs['money']/100;		
		return $rs;
	}

	public function has_account_info($uid, $start, $end)
	{
		if(intval($uid)<=0)
			return array();

		return $this->_merchant_money->has_info($uid, $start, $end);
	}

	/**
	 *	修改某条充值费用信息
	 *	@param int $id
	 *	@param $data= array(
	 *				'start_time'	=> int 
	 *				'end_time'		=> int
	 *				'status'		=> int // 费用状态 0 : 未缴费 1 ： 已缴费 
	 *				'invoice_time'	=> int //发票日期
	 *				'receipt_time'  => int //收据日期
	 *				),
	 *
	 *	@return boolean
	 *
	 */
	public function set_account_info($id, $data)
	{
		if(intval($id)<=0 || empty($data))
			return false;

		$rs = $this->_merchant_money->set_info($id, $data);
		
		return $rs;
	}

	/**
	 * 充值录入 年费、 保证金、预存服务费
	 *
	 * @param String $money_type   Kohana::config('account.money_type.***')
	 * @param int $money   单位：分
	 * @param array $others = array(
	 *							'merchant_uid'
	 *							'shop_id'
	 *							'start_time' => int //开始时间
	 *							'end_time'   => int //结束时间
	 *							'invoice'	 => int //开发票的时间
	 *							'receipt'    => int //收据时间
	 *							'operate_desc' => String //运营描述 
	 *							'operater_man' => String //操作人
	 *							'desc'		 => String // 描述 商家可见
	 *
	 * @return boolean
	 */
	public function recharge_account_money($money_type, $money, $other = array())
	{
		$money_type_name = 'account.money_type.'.$money_type;
		
		$type = Kohana::config($money_type_name);

		$desc = strval($desc);

		if(empty($type) || $money <= 0)
			return false;

		$operate_type = Kohana::config('account.operate_type.recharge');

		$data['merchant_uid']  = $other['merchant_uid'];
        $data['shop_id']       = $other['shop_id'];
		$data['contract_id']   = (int) $other['contract_id'];
		$data['start_time']    = (int) $other['start_time']; 
		$data['end_time']      = (int) $other['end_time'];
		$data['invoice']	   = (int) $other['invoice'];
		$data['receipt']	   = (int) $other['receipt'];
		$data['invoice_status']= (int) $other['invoice_status'];
		$data['desc']          = strval($other['desc']);
		$data['status']		   = (int) $other['status'];
		$data['operate_desc']  = strval($other['operate_desc']);
		$data['operater_man']  = strval($other['operate_man']);		
		
		$rs	= $this->_merchant_money->set_money($operate_type, $type, $money, $data);

		return $rs;
	}

	/**
	 * 扣除预存服务费
	 * 
	 * @param int $shop_id
	 * @param int $money  单位：分
	 * @param String $desc  扣除预存服务费的描述， 如不传，系统会自动添加描述
	 *
	 * @return boolean
	 */
	public function charge_account_server_money($money, $desc = NULL)
	{
		if($money <= 0)
			return false;

		$operate_type = Kohana::config('account.operate_type.charge');

		$money_type = Kohana::config('account.money_type.stored_service');

		$rs	= $this->_merchant_money->set_money($operate_type,$money_type, $money, $desc);

		//发通知
		events::notify(kohana::config('events.merchant_account.charge'), $this);

		return $rs;
	}

	/**
	 * 接受事件并处理
	 *
	 * @param mixed $event
	 * @param mixed $param
	 *
	 * @return void
	 */
	public function handle ($event, & $param)
	{
		if($event == Kohana::events('events.merchant_account'))
		{
			try{
				if(empty($param) || !is_object($event))
					return;

				//开始处理事件 


			}catch(Exception $e)
			{
				//	
			}
		}
	}
	
	private static function instance ($type)
	{
		if(! isset(self::$_instance[$type]))
		{
			$className = 'Account_Merchant_'.$type;
			self::$_instance[$type] = new $className($merchant_uid);
		}
		return self::$_instance[$type];
	}

}
?>
